﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Base.DAL;
using Base.BLL;
using Base.Util;
using System.ComponentModel;
using MPSystem.Models;
using Ay.Framework.WPF;
using ADX.Views;

namespace MPSystem.Views.YFQ
{
    /// <summary>
    /// WYReport_Condition.xaml 的交互逻辑
    /// </summary>
    public partial class WYReport_Condition : Page
    {
        public Dictionary<string, string> Cond = new Dictionary<string, string>();
        public static WYReport_Condition _Me = null;
        public static WYReport_Condition Me
        {
            get
            {
                if (_Me == null)
                {
                    _Me = new WYReport_Condition();
                    _Me.UpdateCond();
                }
                return _Me;
            }
            set { _Me = value; }
        }
        int xdbh = 0;

        public WYReport_Condition()
        {
            InitializeComponent();
            LoadData();
            Me = this;
        }


        public void LoadData()
        {
            #region 加载监测类型
            colJclx.Items.Add("顶板离层");
            colJclx.Items.Add("顶板下沉");
            colJclx.SelectedIndex=0;
            #endregion
            #region 报警输出选择
            colSc.Items.Add("输出全部");
            colSc.Items.Add("输出最后一条");
            colSc.SelectedIndex = 1;
            #endregion
            #region 量程选择
            for (int i = 1; i <= 8; i++)
            {
                cbLc.Items.Add(i*100);
            }
            cbLc.SelectedIndex = 1;
            #endregion
            
            LoadBH();
            Begtime.Date = DateTime.Now.Date;
            Endtime.Date = DateTime.Now.Date + new TimeSpan(23, 59, 59);
        }

        #region 读取传感器列表
        public void LoadBH()
        {
            if (xdbh == ZCYLPage_WY_test.xdbh) return;
            xdbh = ZCYLPage_WY_test.xdbh;
            var li_ylfz = ConfigService.GetDblcWzEnabled(ZCYLPage_WY_test.xdbh, colJclx.SelectedIndex==0);
            Items = new Dictionary<string, object>();
            if (li_ylfz.Count() > 0)
            {
                foreach (var r in li_ylfz)
                {
                    Items.Add(r.CGQBH.ToString(), r.CGQBH.ToString());
                }
            }
            MC.ItemsSource = Items;
            MC.SelectedItems = Items.Clone() as Dictionary<string, object>;
        }
        #endregion

        public void UpdateCond()
        {
            Cond.Clear();
            try
            {
                if (MC.SelectedItems == null)
                {
                    AyMessageBox.ShowError("传感器必须选择！");
                    return;
                }
                //确认多选下拉数据
                string str_List = "";
                foreach (var item in MC.SelectedItems.Keys)
                {
                    if (str_List == "")
                        str_List = item.ToString();
                    else
                        str_List += "," + item.ToString();
                }
                //确认是否显示曲线
                string draw = IfVisible.IsChecked == true ? "1" : "0";

                Cond.Add("lx", colJclx.SelectedIndex.ToString());//传感器类型，0：离层 1：下沉
                Cond.Add("bhs", str_List);
                Cond.Add("dtStart", Begtime.Text.ToString());
                Cond.Add("dtEnd", Endtime.Text.ToString());
                Cond.Add("lc", cbLc.SelectedValue.ToString());//量程
                Cond.Add("count", colSc.SelectedIndex.ToString());//报警输出，0：全部 1：最后一条
                Cond.Add("rzbh", Rzbh.Text.ToString());
                Cond.Add("draw", draw);
                string sign = "";
                if (C1.IsChecked == true) sign += "总工：       ";
                if (C2.IsChecked == true) sign += "生产科：       ";
                if (C3.IsChecked == true) sign += "区队：       ";
                if (C4.IsChecked == true) sign += "报表人：       ";
                if (C5.IsChecked == true) sign += TYj.Text + "：";
                Cond.Add("sign", sign);
            }
            catch { }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {            
            this.CloseParentWindow();
        }

        private void C5_Click(object sender, RoutedEventArgs e)
        {
            if (C5.IsChecked == true)
                TYj.IsEnabled = true;
            else
                TYj.IsEnabled = false;
        }
        #region 多选下拉框使用
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        private Dictionary<string, object> _items;
        private Dictionary<string, object> _selectedItems;
        public Dictionary<string, object> Items
        {
            get
            {
                return _items;
            }
            set
            {
                _items = value;
                NotifyPropertyChanged("Items");
            }
        }
        public Dictionary<string, object> SelectedItems
        {
            get
            {
                return _selectedItems;
            }
            set
            {
                _selectedItems = value;
                NotifyPropertyChanged("SelectedItems");
            }
        }
        private void Submit()
        {
            foreach (KeyValuePair<string, object> s in SelectedItems)
                MessageBox.Show(s.Key);
        }

        #endregion

        private void ColJclx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var li_ylfz = ConfigService.GetDblcWzEnabled(ZCYLPage_WY_test.xdbh, colJclx.SelectedIndex == 0);
            Items = new Dictionary<string, object>();
            if (li_ylfz.Count() > 0)
            {
                foreach (var r in li_ylfz)
                {
                    Items.Add(r.CGQBH.ToString(), r.CGQBH.ToString());
                }
            }
            MC.ItemsSource = Items;
            MC.SelectedItems = Items.Clone() as Dictionary<string, object>;
        }
    }
}
